package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.ButterKnife;
import cn.com.ddapp.ddm.MainActivity;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.bean.HomeBean;
import cn.com.ddapp.ddm.util.TDevice;

/**
 * Created by Administrator on 2016/8/17 0017.
 */
public class ClassifyAdapter extends RecyclerView.Adapter<ClassifyAdapter.MyHolder> {

    private Context context;
    private List<HomeBean.SortListBean> sortListBeen;
    private LayoutInflater inflater;

    public ClassifyAdapter(Context context,List<HomeBean.SortListBean> sortListBeen) {
        this.context = context;
        this.sortListBeen=sortListBeen;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_classify_goods, parent, false);
        AutoUtils.auto(view);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
//        Log.i("aaa","---bb--"+position);
        if(position==9){
            holder.imaView.setImageResource(R.mipmap.more);
            holder.textView.setText("更多");
            holder.imaView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    togo();
                }
            });

        }else{


        int width=(int)(TDevice.getScreenWidth()-80) /6;
//        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(width,width);
//        holder.imaView.setLayoutParams(params);
        holder.imaView.getLayoutParams().width=width;
        holder.imaView.getLayoutParams().height=width;
            String imgL=sortListBeen.get(position).getImg();
//        String []nUrl=imgL.split("\\.");
//            StringBuffer buffer=new StringBuffer();
//            buffer.append(nUrl[0]).append("_360.jpg");
        Glide.with(context).load(OKUtilAPI.imgBaseUrl+imgL).into(holder.imaView);

            holder.textView.setText(sortListBeen.get(position).getName());

        holder.llRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position);
                }
            }
        });

        holder.llRoot.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemLongClick(v, position);
                }
                return true;
            }
        });
        }
    }

    private void togo() {
        ((MainActivity)context).showSortPageFragment();
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v, int position);
    }
    @Override
    public int getItemCount() {
        return 10;
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private ImageView imaView;
        private TextView textView;
        private RelativeLayout llRoot;

        public MyHolder(View itemView) {
            super(itemView);
            imaView = ((ImageView) itemView.findViewById(R.id.imaView));
            textView = ((TextView) itemView.findViewById(R.id.textView));
            llRoot = ((RelativeLayout) itemView.findViewById(R.id.llRoot));
            ButterKnife.bind(this,itemView);
        }
    }
}
